package handler

import (
	"github.com/gin-gonic/gin"
	"zg3zk3/api/request"
	"zg3zk3/interface/dao"
	"zg3zk3/interface/models"
	"zg3zk3/pkg"
)

func Register(context *gin.Context) {

	var req request.RegisterReq
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(200, gin.H{
			"code": "code",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	register := models.Users{
		Id:       req.Id,
		Phone:    req.Phone,
		Password: req.Password,
	}

	dao.CreateRegister(&register, &register)

	if register.Id == 0 {
		context.JSON(200, gin.H{
			"code": "code",
			"msg":  "注册成功",
			"data": nil,
		})
		return
	}

	context.JSON(200, gin.H{
		"code": "code",
		"msg":  "注册成功",
		"data": nil,
	})

}

func Login(context *gin.Context) {
	var req request.LoginReq
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(200, gin.H{
			"code": "code",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var login models.Users
	dao.FindLogin(&login, &login)

	token, err := pkg.NewJWT("jwt").CreateToken(pkg.CustomClaims{
		ID: login.Id,
	})
	if err != nil {
		return
	}
	context.JSON(200, gin.H{
		"code": "code",
		"msg":  "登录成功",
		"data": token,
	})

}
